/**
 * [128] 最长连续序列
 *
 * @format
 * @lc app=leetcode.cn id=128 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function (nums) {
    let max = 0
    const dist = {}
    for (let i = 0; i < nums.length; i++) dist[nums[i]] = true
    for (let i = 0; i < nums.length; i++) {
        if (!dist[nums[i] - 1]) {
            let [len, j] = [1, nums[i]]
            while (true) {
                if (dist[++j]) len++
                else break
            }
            if (len > max) max = len
        }
    }
    return max
}
// debugger
// const nums = [100, 4, 200, 1, 3, 2]
// const MAX = longestConsecutive(nums)
// var longestConsecutive = function (nums) {
//     if (nums.length == 0) return 0
//     const dist = {}
//     let [min, max] = [nums[0], nums[0]]
//     for (let i = 0; i < nums.length; i++) {
//         dist[nums[i]] = true
//         if (nums[i] > max) max = nums[i]
//         if (nums[i] < min) min = nums[i]
//     }
//     let [len, maxLen] = [0, 0]
//     for (let i = min; i <= max; i++) {
//         if (len > maxLen) maxLen = len
//         dist[i] ? len++ : (len = 0)
//     }
//     return len > maxLen ? len : maxLen
// }
// @lc code=end
